import React from "react";
import { findDOMNode } from "react-dom";

export interface ComponentProps{
    className?:string
}

/**
 * 包装的组件类型，自动添加className
 */
export default class Component<T = {}, S = {}> extends React.Component<T & ComponentProps, S>{
    componentDidMount() {
        if (this.props.className) {
          const domNode : Element = findDOMNode(this) as Element;
          if (domNode instanceof Element){
            const newClassName = domNode.className + " " + this.props.className;
            domNode.className = newClassName;
          }
        }
    }
}

/**
 *  函数形式的包装
 */
export function componentClass(props:ComponentProps, className:string = ''){
    return className + ' ' + (props.className ? props.className : '');
}